/**
 * Created by admin on 2017/7/13.
 */
(function () {
    var module = {};
    var tools = Tools();
    var isIdentification;

    module.init=function () {
        tools.isOpenAccount();
        tools.getUserInfo(); 
        //查询用户名
        tools.doGet(accountURL + tools.apiURL.queryUserInfo, {}, module.queryUserInfo);
        //获取银行卡信息
        tools.doGet(accountURL+tools.apiURL.bankCard,{},module.bankCard);
        //测试结论
        tools.doGet(accountURL+tools.apiURL.testResult,{},module.testResult);
        module.checkAll();

        //交易密码是否存在
      tools.doPost(accountURL +tools.apiURL.tradePasswordExists,{},module.tradePasswordExists);

    };
    function isEmptyObject(e) {
        var t;
        for (t in e)
            return !1;
        return !0
    }
    // 0:存在 ；其他都是不存在 交易密码
    module.tradePasswordExists=function (data) {
        if(data.status==0){
            // $('#setTradePw').append('<dd id="setTradePw"> <form id="J_SetTradePassword" name="setTradePassword" novalidate="novalidate"> <div class="formClass"> <ul> <li><input type="submit" id="J_SetTradePasswordSubmit" class="inputBut showL" value="修改密码"> <input type="submit" id="J_SetTradePasswordSubmit1" class="inputBut showL" value="忘记密码"></li> </ul> </div> </form> </dd>')
             $('#unPassword').hide();
            //修改
            $(document).on('click', '#updatePassword', function () {
                tools.doPost(accountURL +tools.apiURL.tradePasswordExists,{isForget:false},function (data) {
                    if(data.status==0){
                        tools.formPost(data.data.actionUrl, data.data.parms);
                    }else{
                        $("#alertMessage").fadeIn();
                        $("#message").text(data.message);
                        setTimeout(function(){
                            $("#alertMessage").fadeOut();
                        },2000)
                    }
                });
            });
            //忘记
            $(document).on('click', '#forgetPassword', function () {
                tools.doPost(accountURL +tools.apiURL.tradePasswordExists,{isForget:true},function (data) {
                    if(data.status==0){
                        tools.formPost(data.data.actionUrl, data.data.parms);
                    }else{
                        $("#alertMessage").fadeIn();
                        $("#message").text(data.message);
                        setTimeout(function(){
                            $("#alertMessage").fadeOut();
                        },2000)
                    }
                });
            })
        }else{
            $('#setTradePw').remove();
            $('#unPassword').show();
            $('.jyPassword').attr('href','openAccount.html');
        }
        module.checkAll();
    };
    module.testResult=function (data) {
      if(data.status==0){
          if(isEmptyObject(data.data)){
              $('#textInfo').text('快来测一测，让您的投资聪明起来！');
              $('#textGo').append('<span>马上测试</span>')
          }else{
              if(data.data.times==0){
                  $('#textInfo').text('快来测一测，让您的投资聪明起来！');
                  $('#textGo').append('<span>马上测试</span>')
              }else if(data.data.times==1){
                  $('#textInfo').text('您是'+data.data.result);
                  $('#textGo').append('<span>重新测试</span>')
              }else{
                  $('#textGo').hide();
                  $('#textInfo').text('您是'+data.data.result);
              }
          }
      }
    };
    
    module.bankCard=function (data) {
        if (data.status==0) {
            if(data.data!=null){
                var content = data.data;
                if(content.status==2){
                    $('#isIdent').hide();
                    $('#unIdent').show();
                    $('#toSafetyBank').text('未认证');
                    $('#openOrBank').attr('href','openAccount.html')
                }else if(content.status==0){
                    $('#isIdent').hide();
                    $('#unIdent').show();
                    $('#toSafetyBank').text('认证中');
                }else if(content.status==1){
                    $('#isIdent').show();
                    $('#unIdent').hide();
                }
            }else{
                $('#isIdent').hide();
                $('#unIdent').show();
                $('#toSafetyBank').text('未认证');
                $('#openOrBank').attr('href','bankCard.html')
            }
        }

    };

    //查询用户名
    module.queryUserInfo = function (data) {
        if(data.status==0){
            //用户名
            $('#userName').text(data.data.username);
        }

    };


    module.checkAll=function () {
        $changePwdForm = $("#J_ChangePassword");
        $changePwdSubmit = $("#J_ChangePasswordSubmit");
        $password = $("#J_NewPassword");
        $oldPassword = $("#J_OldPassword");


        $setTradePwdForm = $("#J_SetTradePassword");
        $setTradePwdSubmit = $("#J_SetTradePasswordSubmit");
        $tradePassword = $("#J_TradePassword");

        $changeTradePwdForm = $("#J_ChangeTradePassword");
        $changeTradePwdSubmit = $("#J_ChangeTradePasswordSubmit");
        $newTradePassword = $("#J_NewTradePassword");
        $oldTradePassword = $("#J_OldTradePassword");

        $.validator.addMethod("notEqualTo", function (value, element, param) {
            if ($(param).val() == value) {
                return false;
            } else {
                return true;
            }
        });

        $.validator.addMethod("validIdentity", function (value, element) {
            return idCardValidate(value);
        });

        // 修改登录密码表单验证
        $changePwdForm.validate({
            rules: {
                oldPassword: "required",
                newPassword: {
                    required: true,
                    pattern: /^[^\s&\"<>]+$/,
                    minlength: 6,
                    maxlength: 18,
                    notEqualTo: $oldPassword
                },
                confirmPassword: {
                    required: true,
                    equalTo: "#J_NewPassword"
                }
            },
            messages: {
                oldPassword: {
                    required: "您还未填写原登陆密码"
                },
                newPassword: {
                    required: "您还未填写新的登陆密码",
                    pattern: "密码不能包含下述字符：<>\"&",
                    minlength: "密码长度必须是6~18位",
                    maxlength: "密码长度必须是6~18位",
                    notEqualTo: "新密码不能和原密码相同，请重新输入"
                },
                confirmPassword: {
                    required: "您还未确认填写新的登陆密码",
                    equalTo: "新密码和确认密码不一致"
                }
            },
            submitHandler: function () {
                tools.doPost(accountURL +tools.apiURL.updateUserPwd, {
                    oldUserPwd: $("#J_OldPassword").val(),
                    newUserPwd: $("#J_NewPassword").val()
                }, module.updateUserPwd);
            }
        });
    };
    module.resetTradePassword=function (data) {
        if(data.status==0){
            alert('');
        }else{
            alert(data.message);
            $("#J_TradePassword").val('');
        }
    };
    module.updateUserPwd=function (data) {
        if(data.status==0){
            alert('密码已更新，请保管好您的新密码！');
            $('.updatePsHide').hide();
        }else{
            alert(data.message);
        }
        $("#J_OldPassword").val('');
        $("#J_NewPassword").val('');
        $("#J_ConfirmPassword").val('');
    };
    module.init();
})();
